package com.smarthome.web.property.controller.system;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smarthome.core.advicemanager.PlatformAdviceService;
import com.smarthome.domain.bean.ShPlatformNotice;
import com.smarthome.domain.bean.ShUser;
import com.smarthome.domain.common.Result;
import com.smarthome.domain.enums.AdviceStatusEnum;
import com.smarthome.domain.enums.PushMobileType;
import com.smarthome.domain.vo.boss.Combobox;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;
import com.smarthome.rpc.PushMobileMessageService;
import com.smarthome.web.property.utils.UserHolder;

/**
 * 
* @ClassName: PlatformAdviceController
* @Description: TODO(平台公告管理)
* @author cyj
* @date 2015年9月21日 下午10:33:55
*
 */
@Controller
@RequestMapping("/platformAdviceController")
public class PlatformAdviceController {
	
	@Resource
    private PlatformAdviceService platformAdviceService;
	
	@Resource
	private PushMobileMessageService pushMobileMessageService;
	
	/**
	 * 
	* @Title: getPage 
	* @Description: TODO(根据条件获取平台公告数据列表) 
	* @param company
	* @param helper
	* @param request
	* @return
	* DataGrid
	* @throws 
	* @author cyj
	 */
	@RequestMapping("/dataGrid")
    @ResponseBody
	public DataGrid getPage(ShPlatformNotice platformNotice,PageHelper helper,HttpServletRequest request){
		String startRecord = request.getParameter("startRecord");
		String endRecord = request.getParameter("endRecord");
		String startPublish = request.getParameter("startPublish");
		String endPublish = request.getParameter("endPublish");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		if(StringUtils.isNotBlank(startRecord)) {
    		try {
    			platformNotice.addQueryData("startRecordTime", sdf.parse(startRecord));
			} catch (ParseException e) {
				e.printStackTrace();
			}
    	}
		if(StringUtils.isNotBlank(endRecord)) {
    		try {
    			platformNotice.addQueryData("endRecordTime", sdf.parse(endRecord));
			} catch (ParseException e) {
				e.printStackTrace();
			}
    	}
		
		if(StringUtils.isNotBlank(startPublish)) {
    		try {
    			platformNotice.addQueryData("startPublishTime", sdf.parse(startPublish));
			} catch (ParseException e) {
				e.printStackTrace();
			}
    	}
		if(StringUtils.isNotBlank(endPublish)) {
    		try {
    			platformNotice.addQueryData("endPublishTime", sdf.parse(endPublish));
			} catch (ParseException e) {
				e.printStackTrace();
			}
    	}
		
		return platformAdviceService.qeuryDataGrid(platformNotice, helper);
	}
	
	/**
	 * 
	* @Title: addPage 
	* @Description: TODO(跳转新增页面) 
	* @param request
	* @return
	* String
	* @throws 
	* @author cyj
	 */
	@RequestMapping("/addPage")
    public String addPage(HttpServletRequest request) {
        return "/system/adviceManager/platformAdviceAdd";
	}
	
	
	
	/**
	 * 
	* @Title: add 
	* @Description: TODO(新增平台公告) 
	* @param platformNotice
	* @return
	* Result
	* @throws 
	* @author cyj
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Result add(ShPlatformNotice platformNotice) {
		if(StringUtils.isBlank(platformNotice.getTitle())) {
			return Result.failure("请输入标题！");
		}
		if(platformNotice.getStatus() == null) {
			return Result.failure("请选择状态！");
		}
		if(StringUtils.isBlank(platformNotice.getContent())) {
			return Result.failure("请输入内容！");
		}
		Date date = new Date();
		try{
			//获取登录的系统用户
	    	ShUser shUser = UserHolder.getUser();
			platformNotice.setRecordUserId(shUser.getId());
			platformNotice.setGmtCreate(date);
			platformNotice.setGmtModify(date);
			platformNotice.setRecordTime(date);
			if(AdviceStatusEnum.PUBLISHED.getCode() == platformNotice.getStatus()) {
				platformNotice.setPublishTime(date);
				pushMobileMessageService.pushMsgToAll(platformNotice.getTitle(), PushMobileType.PLATFORM.getCode());
			}
			platformAdviceService.add(platformNotice);
		} catch(Exception e){
			e.printStackTrace();
			return Result.failure(e.getMessage());
			  
		}
		return Result.success("新增成功！");
	 }
	  
	/**
	 * 
	* @Title: editPage 
	* @Description: 调整修改页面 
	* @param request
	* @param id
	* @return
	* String
	* @throws 
	* @author cyj
	 */
	@RequestMapping("/editPage")
	public String editPage(HttpServletRequest request,Long id){
		request.setAttribute("platformNotice", platformAdviceService.getById(id));
		return "/system/adviceManager/platformAdviceEdit";
	}
	
	
	@RequestMapping("/edit")
	@ResponseBody
	public Result edit(ShPlatformNotice platformNotice) {
		if(StringUtils.isBlank(platformNotice.getTitle())) {
			return Result.failure("请输入标题！");
		}
		if(platformNotice.getStatus() == null) {
			return Result.failure("请选择状态！");
		}
		if(StringUtils.isBlank(platformNotice.getContent())) {
			return Result.failure("请输入内容！");
		}
		Date date = new Date();
		try{
			ShPlatformNotice notice = platformAdviceService.getById(platformNotice.getId());
			
			BeanUtils.copyProperties(platformNotice, notice);
			//获取登录的系统用户
	    	ShUser shUser = UserHolder.getUser();
	    	notice.setRecordUserId(shUser.getId());
	    	notice.setGmtModify(date);
	    	notice.setRecordTime(date);
			if(AdviceStatusEnum.PUBLISHED.getCode() == notice.getStatus()) {
				notice.setPublishTime(date);
				pushMobileMessageService.pushMsgToAll(notice.getTitle(), PushMobileType.PLATFORM.getCode());
			}else {
				notice.setPublishTime(null);
			}
			platformAdviceService.add(notice);
		} catch(Exception e){
			e.printStackTrace();
			return Result.failure(e.getMessage());
			  
		}
		return Result.success("修改成功！");
	 }
	
	
	/**
	 * 
	* publish(发布平台公告)
	* @Title: publish
	* @Description: TODO
	* @param platformNotice
	* @return
	* Result
	* @throws
	* @author cyj
	* @date 2015年9月23日 下午9:25:24
	 */
	  /*@RequestMapping("/publish")
	  @ResponseBody
	  public Result publish(ShPlatformNotice platformNotice) {
		  if(platformNotice.getStatus().equals(AdviceStatusEnum.PUBLISHED.getCode())) {
			  return Result.failure("发布失败！该公告已经发布！");
		  }
		  try{
			  int count = platformAdviceService.publish(platformNotice);
			  if(count == 0) {
				  return Result.failure("发布失败！该公告已被删除！");
			  }
		  } catch(Exception e){
			  return Result.failure("发布失败！系统异常！");
		  }
		  return Result.success("发布成功！");
		  
	  }*/
	  
	  
	  /**
	   * 
	  * @Title: delete 
	  * @Description: TODO(删除平台公告) 
	  * @param id
	  * @return
	  * Result
	  * @throws 
	  * @author cyj
	   */
	  @RequestMapping("/delete")
	  @ResponseBody
	  public Result delete(Long id) {
		  try{
			  platformAdviceService.delete(id);
		  } catch(Exception e){
			  return Result.failure("公告被引用，不能删除");
		  }
		  return Result.success("删除成功！");
	  }
	  
	  /**
	   * 
	  * getComboData(获取平台公告管理需要的combo数据)
	  * @Title: getComboData
	  * @Description: TODO
	  * @return
	  * Map<String,Object>
	  * @throws
	  * @author cyj
	  * @date 2015年9月22日 下午8:24:47
	   */
	  @RequestMapping("/getComboData")
	  @ResponseBody
	  public Map<String, Object> getComboData() {
	  	  Map<String,Object> result = new HashMap<>();
    	  List<Combobox> status = platformAdviceService.status(false);
    	  result.put("status", status);
    	  return result;
	  }
	
}
